#include "shoppingcart_ui.h"
#include"shoppingcart_ui_logic.h"
#include <QIcon>
#include <QPixmap>

ShoppingCartUI::ShoppingCartUI(QWidget *parent)
    : QWidget(parent), mainLayout(new QGridLayout(this))
{
    setupUi();
}

/*void ShoppingCartUI::setupUi()
{
    //checkboxes.clear();
    scrollArea = new QScrollArea;
    scrollArea->setWidgetResizable(true);
    QWidget *scrollWidget = new QWidget;
    scrollWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    gridLayout = new QGridLayout(scrollWidget);
    gridLayout->setAlignment(Qt::AlignTop);

    // Add table headers
    QLabel *headerImage = new QLabel("商品图片");
    QLabel *headerName = new QLabel("名称");
    QLabel *headerPrice = new QLabel("价格");
    QLabel *headerQuantity = new QLabel("数量");
    QLabel *headerActions = new QLabel("已选择");
    QPushButton *mage = new QPushButton("管理购物车");

    int row = 0;
    gridLayout->addWidget(headerImage, row, 0);
    gridLayout->addWidget(headerName, row, 1);
    gridLayout->addWidget(headerPrice, row, 2);
    gridLayout->addWidget(headerQuantity, row, 3);
    gridLayout->addWidget(headerActions, row, 4);
    gridLayout->addWidget(mage, row, 5);

    scrollArea->setWidget(scrollWidget);
    mainLayout->addWidget(scrollArea, 0, 0);

    // Add checkout button
    checkoutButton = new QPushButton("结算");
    mainLayout->addWidget(checkoutButton, 1, 0, 1, -1);

    setLayout(mainLayout);
}*/
QMap<QString, QStringList> ShoppingCartUI::getSelectedItems()
{
    QMap<QString, QStringList> selectedItems;
    for (QCheckBox *checkbox : checkboxes)
    {
        if (checkbox->isChecked())
        {
            QString name = checkbox->property("productName").toString();
            QString price = checkbox->property("productPrice").toString();
            QString quantity = checkbox->property("productQuantity").toString();
            QString productId = checkbox->property("productId").toString();
            // 使用 QStringList 存储 price 和 quantity
            QStringList details;
            details << price << quantity << productId;

            // 将商品名称与详情（价格和数量）插入到 selectedItems 中
            selectedItems.insert(name, details);
        }
    }
    return selectedItems;
}

void ShoppingCartUI::setupUi()
{
    scrollArea = new QScrollArea;
    scrollArea->setWidgetResizable(true);
    QWidget *scrollWidget = new QWidget;
    scrollWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    // Use QVBoxLayout instead of QGridLayout
    verticalLayout = new QVBoxLayout(scrollWidget);
    verticalLayout->setAlignment(Qt::AlignTop);

    // Add table headers
    QLabel *headerImage = new QLabel("商品图片");
    QLabel *headerName = new QLabel("名称");
    QLabel *headerPrice = new QLabel("价格");
    QLabel *headerQuantity = new QLabel("数量");
    QLabel *headerActions = new QLabel("已选择");
    QPushButton *mage = new QPushButton("管理购物车");

    QHBoxLayout *headerLayout = new QHBoxLayout();
    headerLayout->addWidget(headerImage);
    headerLayout->addWidget(headerName);
    headerLayout->addWidget(headerPrice);
    headerLayout->addWidget(headerQuantity);
    headerLayout->addWidget(headerActions);
    headerLayout->addWidget(mage);

    verticalLayout->addLayout(headerLayout);

    scrollArea->setWidget(scrollWidget);
    mainLayout->addWidget(scrollArea, 0, 0);

    // Add checkout button
    checkoutButton = new QPushButton("结算");
    mainLayout->addWidget(checkoutButton, 1, 0, 1, -1);

    setLayout(mainLayout);
}
ShoppingCartUI::~ShoppingCartUI() {}
